<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\MemberCreateRequest;
use App\Http\Requests\MemberUpdateRequest;
use App\Library\R;
use App\Models\Member;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MemberController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.member.index');
    }

    public function data(Request $request)
    {
        $model = Member::query()->with(['driver', 'owner', 'relation']);
        if ($request->get('name')) {
            $model = $model->where('name', 'like', '%' . $request->get('name') . '%');
        }
        if ($request->get('phone')) {
            $model = $model->where('phone', 'like', '%' . $request->get('phone') . '%');
        }
        $res = $model->orderBy('created_at', 'desc')->paginate($request->get('limit', 30))->toArray();

        return $this->table($res['data'], $res['total']);
    }

    /**
     * Show the form for creating a new resource.
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.member.create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(MemberCreateRequest $request)
    {
        $data = $request->all();

        $data['uuid'] = \Faker\Provider\Uuid::uuid();

        if (Member::create($data)) {
            return redirect()->to(route('admin.member'))->with(['status' => '添加账号成功']);
        }
        return redirect()->to(route('admin.member'))->withErrors('系统错误');
    }

    /**
     * Show the form for editing the specified resource.
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $member = Member::findOrFail($id);
        return view('admin.member.edit', compact('member'));
    }

    /**
     * 修改用户
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(MemberUpdateRequest $request, $id)
    {
        $member = Member::findOrFail($id);
        $data = $request->only(['name', 'phone', 'avatar']);

        if ($member->update($data)) {
            return redirect()->to(route('admin.member'))->with(['status' => '更新用户成功']);
        }
        return redirect()->to(route('admin.member'))->withErrors('系统错误');
    }

    /**
     * 禁用用户
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)) {
            return $this->error('请选择禁用项');
        }
//        if (Member::destroy($ids)) {
//            return $this->success('删除成功');
//        }
        if (Member::whereIn('id',$ids)->update(['status' => Member::STATUS_N])) {
            return $this->success('禁用成功');
        }
        return $this->error('禁用失败');
    }

    /**
     * 启用用户
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function usable(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)) {
            return $this->error('请选择启用项');
        }
        if (Member::whereIn('id',$ids)->update(['status' => Member::STATUS_Y])) {
            return $this->success('启用成功');
        }
        return $this->error('启用失败');
    }
}
